<template>
  <div>
    <div
      class="project"
      v-if="
        !productLibraryAddShow && !ServiceAddShow && !ProductLibrarydetailShow
      "
    >
      <!-- 筛选按钮 -->
      <div class="search-button">
        <el-button
          v-if="route.name === 'ProductLibrary'"
          v-hasPermi="['system:productLibrary:query']"
          class="btn search"
          @click="getProjectList"
          ><el-icon class="icon" color="#fff"> <Search /> </el-icon
          >查询</el-button
        >
        <el-button
          v-if="route.name === 'ProductLibraryCheck'"
          v-hasPermi="['system:productLibraryCheck:query']"
          class="btn search"
          @click="getProjectList"
          ><el-icon class="icon" color="#fff"> <Search /> </el-icon
          >查询</el-button
        >
        <el-button
          v-hasPermi="['system:productLibrary:add']"
          class="btn search"
          @click="
            productLibraryAddShow = true;
            pageType = 'add';
          "
          ><el-icon class="icon" color="#fff"> <Plus /> </el-icon
          >新增</el-button
        >
        <el-button class="btn add" @click="handleRefresh"
          ><el-icon class="icon" color="#fff"> <Refresh /> </el-icon
          >重置</el-button
        >
      </div>
      <!-- 筛选条件 -->
      <div class="search">
        <div class="search-item">
          <el-input
            clearable
            v-model.trim="params.droneBrand"
            class="search-input"
            maxlength="40"
            placeholder="请输入无人机品牌"
          ></el-input>
        </div>
        <div class="search-item">
          <el-input
            clearable
            v-model.trim="params.droneType"
            class="search-input"
            maxlength="40"
            placeholder="请输入无人机型号"
          ></el-input>
        </div>
        <div class="search-item">
          <el-select
            v-model="params.netModuleFlag"
            placeholder="请选择是否装4G模块"
            clearable
            class="search-input"
          >
            <el-option
              v-for="dict in statusList"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </div>
        <div class="search-item">
          <el-select
            ref="refSelect"
            v-model="params.belongDepartment"
            placeholder="请选择归属部门"
            clearable
            class="search-input"
          >
            <!-- @change="handleDepart" -->
            <el-option
              v-for="dict in deptList"
              :key="dict.deptId"
              :label="dict.deptName"
              :value="dict.deptId"
            />
          </el-select>
        </div>
        <div class="search-item">
          <el-input
            clearable
            v-model.trim="params.droneSerialNumber"
            class="search-input"
            placeholder="请输入无人机SN"
            maxlength="40"
          ></el-input>
        </div>
      </div>

      <!-- 表格数据 -->
      <el-table
        v-loading="loading"
        :data="projectList"
        border
        height="64vh"
        :header-cell-style="{ 'text-align': 'center' }"
      >
        <!-- 操作模式 -->
        <el-table-column
          label="操作"
          :show-overflow-tooltip="true"
          width="180px"
        >
          <template #default="{ row }">
            <div class="table-operate">
              <el-icon
                v-if="route.name === 'ProductLibrary'"
                v-hasPermi="['system:productLibrary:detail']"
                title="查看详情"
                color="#007CEF"
                class="table-icon"
                :size="20"
                @click="handleOperate('detail', row)"
              >
                <Document />
              </el-icon>
              <el-icon
                v-if="route.name === 'ProductLibraryCheck'"
                v-hasPermi="['system:productLibraryCheck:detail']"
                title="查看详情"
                color="#007CEF"
                class="table-icon"
                :size="20"
                @click="handleOperate('detail', row)"
              >
                <Document />
              </el-icon>
              <el-icon
                v-if="route.name === 'ProductLibrary'"
                v-hasPermi="['system:productLibrary:edit']"
                title="编辑"
                color="#FFB700"
                class="table-icon"
                :size="20"
                @click="handleOperate('edit', row)"
              >
                <Edit />
              </el-icon>
              <el-icon
                v-if="route.name === 'ProductLibraryCheck'"
                v-hasPermi="['system:productLibraryCheck:edit']"
                title="编辑"
                color="#FFB700"
                class="table-icon"
                :size="20"
                @click="handleOperate('edit', row)"
              >
                <Edit />
              </el-icon>
              <el-icon
                v-if="route.name === 'ProductLibrary'"
                v-hasPermi="['system:productLibrary:maintenance']"
                title="维修"
                color="#FFB700"
                class="table-icon"
                :size="20"
                @click="handleOperate('maintenance', row)"
                ><Tools />
              </el-icon>
              <el-icon
                v-if="route.name === 'ProductLibrary'"
                v-hasPermi="['system:productLibrary:remove']"
                title="删除"
                color="#E33838"
                class="table-icon"
                :size="20"
                @click="handleOperate('delete', row)"
              >
                <Delete />
              </el-icon>
            </div> </template
        ></el-table-column>
        <el-table-column
          type="index"
          label="序号"
          :index="indexMethod"
          width="70"
          align="center"
        />
        <el-table-column
          label="无人机品牌"
          prop="droneBrand"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          label="无人机型号"
          prop="droneType"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          label="无人机SN"
          prop="droneSerialNumber"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          label="是否装4G模块"
          prop="netModuleFlag"
          :show-overflow-tooltip="true"
        >
          <template #default="{ row }">
            <span>{{ row.netModuleFlag == 0 ? "是" : "否" }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="归属部门"
          prop="belongDepartment"
          :show-overflow-tooltip="true"
          >
        <template #default="{ row }">
            <span>{{ row.belongDepartment ? row.belongDepartment : "--" }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="无人机技术参数"
          prop="technicalParameters"
          :show-overflow-tooltip="true"
        >
          <template #default="{ row }">
            <span class="ellipsisStyle">{{
              row.technicalParameters ? row.technicalParameters : "--"
            }}</span>
          </template></el-table-column
        >
        <el-table-column
          label="卫星导航系统"
          prop="satelliteNavigationSystem"
          :show-overflow-tooltip="true"
        />
      </el-table>

      <!-- 分页 -->
      <pagination
        class="pagination"
        :total="total"
        v-model:page="params.pageNum"
        v-model:limit="params.pageSize"
        @pagination="getProjectList"
      />
    </div>
    <productLibraryAdd
      v-if="productLibraryAddShow"
      ref="productLibraryAddRef"
      v-model:productLibraryAddShow="productLibraryAddShow"
      @refreshList="getProjectList"
      :pageType="pageType"
    />
    <ServiceAdd
      v-if="ServiceAddShow"
      ref="ServiceAddRef"
      v-model:ServiceAddShow="ServiceAddShow"
      @refreshList="getProjectList"
      :pageType="pageType"
    />
    <ProductLibrarydetail
      v-show="ProductLibrarydetailShow"
      ref="ProductLibrarydetailRef"
      v-model:ProductLibrarydetailShow="ProductLibrarydetailShow"
      @refreshList="getProjectList"
      :pageType="pageType"
    />
  </div>
</template>

<script setup>
import productLibraryAdd from "./productLibraryAdd.vue";
import ServiceAdd from "./ServiceAdd.vue";
import ProductLibrarydetail from "./productLibrarydetail.vue";
import {
  getproductEstimateListApi,
  getdronemodifyApi,
  getdronedetailApi,
  getDeptListApi,
} from "@/api/operation/productLibrary.js";
import { getDictsApi } from "@/api/dict";
import { ElMessage, ElMessageBox } from "element-plus";
// 路由
const route = useRoute();
// 搜索参数
const params = reactive({
  droneBrand: "",
  droneType: "",
  droneSerialNumber: "",
  netModuleFlag: "",
  belongDepartment: "",
  pageNum: 1,
  pageSize: 50,
});
// 表格加载效果
const loading = ref(false);
// 项目新增组件显隐
const productLibraryAddShow = ref(false);
// 新增维修
const ServiceAddShow = ref(false);
//详情组件的显隐
const ProductLibrarydetailShow = ref(false);
// 跟进新增组件显隐
const followAddShow = ref(false);
// 列表数据
const projectList = ref([]);
// 总数
const total = ref(0);
// 无人机组件引用
const productLibraryAddRef = ref(null);
// 无人机详情组件引用
const ProductLibrarydetailRef = ref(null);
// 维修组件引用
const ServiceAddRef = ref(null);
// 操作类型
const pageType = ref("");
// 审核状态列表
const checkList = ref([]);
// 是否签合同列表
const contractList = ref([]);
// 归属部门下拉
const deptList = ref([]);
// 是否加装4G模块下拉
const statusList = ref([]);
// 项目类型列表
const typeList = ref([]);

/**
 * @description: 重置
 */
 const handleRefresh = () => {
  Object.assign(params, {
    droneBrand: "",
  droneType: "",
  droneSerialNumber: "",
  netModuleFlag: "",
  belongDepartment: "",
    pageNum: 1,
    pageSize: 50,
  });
  getProjectList()
};


/**
 * @description: 获取项目列表
 */
const getProjectList = async () => {
  try {
    loading.value = true;
    let { pageNum, pageSize } = params;
    const res = await getproductEstimateListApi({ pageNum, pageSize }, params);
    projectList.value = res.rows;
    total.value = res.total;
    loading.value = false;
  } catch {
    loading.value = false;
  }
};

/**
 * @description: 表格操作
 * @param {String} type 类型
 * @param {Object} row  当前行数据
 */
const handleOperate = async (type, row) => {
  switch (type) {
    case "edit":
      pageType.value = type;
      productLibraryAddShow.value = true;
      nextTick(() => {
        productLibraryAddRef.value.handleDetailInfo(row.droneId);
      });
      break;
    case "maintenance":
      ServiceAddShow.value = true;
      pageType.value = type;
      nextTick(() => {
        ServiceAddRef.value.handlemaintenance(row.droneId);
      });
      break;
    case "detail":
      pageType.value = type;
      ProductLibrarydetailShow.value = true;
      nextTick(() => {
        ProductLibrarydetailRef.value.handleDetailInfo(row.droneId);
      });
      break;
    case "delete":
      ElMessageBox.confirm("此操作将删除该项目，是否继续？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        confirmButtonClass: "confirmBtnClass",
        cancelButtonClass: "cancelBtnClass",
        type: "warning",
      })
        .then(async () => {
          let obj = await ProductLibrarydetailRef.value.handleDetailInfo(
            row.droneId
          );
          obj.data.status = 1;
          const res = await getdronemodifyApi(obj.data);
          if (res.code === 200) {
            ElMessage.success(res.msg);
            getProjectList();
          } else {
            ElMessage.error(res.msg);
          }
        })
        .catch(() => {});
      break;

    default:
      break;
  }
};

/**
 * @description: 获取字典
 */
async function getDicts() {
  await getDictsApi("erp_warehousing").then((res) => {
    // 审核状态
    if (res.code === 200) {
      statusList.value = res.data;
    }
  });

  // 运维部门列表
  await getDeptListApi(107).then((res) => {
    if (res.code === 200) {
      deptList.value = res.data;
      // nextTick(() => {
      //   refSelect.value.focus(); // 聚焦
      // });
    }
  });
}

/**
 * @description: 序号
 * @param {Number} index 索引
 */
function indexMethod(index) {
  const currentPage = params.pageNum;
  const pageSize = params.pageSize;
  const truelndex = (currentPage - 1) * pageSize + index + 1;
  return truelndex;
}

onMounted(() => {
  getProjectList();
  getDicts();
});
</script>
<style>
@import "@/assets/styles/confirmBtn.css";
</style>
<style lang="scss" scoped>
.project {
  padding: 20px;
  width: 100%;
  height: 100%;
  .search-item {
    // margin-bottom: 20px;
  }
  .pagination {
    margin-top: 20px;
  }
}
</style>
